<?php

function padd_widget_funct_socialnet() {
	$padd_sb_delicious = unserialize(get_option(PADD_THEME_SLUG . '_sn_username_delicious'));
	$padd_sb_digg = unserialize(get_option(PADD_THEME_SLUG . '_sn_username_digg'));
	$padd_sb_googlebuzz = unserialize(get_option(PADD_THEME_SLUG . '_sn_username_googlebuzz'));
	$padd_sb_facebook = unserialize(get_option(PADD_THEME_SLUG . '_sn_username_facebook'));
	$padd_sb_stumbleupon = unserialize(get_option(PADD_THEME_SLUG . '_sn_username_stumbleupon'));
	$padd_sb_technorati = unserialize(get_option(PADD_THEME_SLUG . '_sn_username_technorati'));
	$padd_sb_feedburner = unserialize(get_option(PADD_THEME_SLUG . '_sn_username_feedburner'));
	$padd_sb_twitter = unserialize(get_option(PADD_THEME_SLUG . '_sn_username_twitter'));
?>
<ul class="socialnet">
	<li class="delicious">
		<a href="<?php echo $padd_sb_delicious; ?>" class="icon-delicious" title="Delicious">
			<img alt="Delicious" src="<?php echo get_stylesheet_directory_uri(); ?>/images/icon-sn-32-delicious.png" />
		</a>
	</li>
	<li class="digg">
		<a href="<?php echo $padd_sb_digg; ?>" class="icon-digg" title="Digg">
			<img alt="Digg" src="<?php echo get_stylesheet_directory_uri(); ?>/images/icon-sn-32-digg.png" />
		</a>
	</li>
	<li class="facebook">
		<a href="<?php echo $padd_sb_facebook; ?>" class="icon-facebook" title="Facebook Profile">
			<img alt="Facebook" src="<?php echo get_stylesheet_directory_uri(); ?>/images/icon-sn-32-facebook.png" />
		</a>
	</li>
	<li class="googlebuzz">
		<a href="<?php echo $padd_sb_googlebuzz; ?>" class="icon-googlebuzz" title="Google Buzz">
			<img alt="Google Buzz" src="<?php echo get_stylesheet_directory_uri(); ?>/images/icon-sn-32-googlebuzz.png" />
		</a>
	</li>
	<li class="stumbleupon">
		<a href="<?php echo $padd_sb_stumbleupon; ?>" class="icon-stumbleupon" title="StumbleUpon">
			<img alt="StumbleUpon" src="<?php echo get_stylesheet_directory_uri(); ?>/images/icon-sn-32-stumbleupon.png" />
		</a>
	</li>
	<li class="technorati">
		<a href="<?php echo $padd_sb_technorati; ?>" class="icon-technorati" title="Technorati">
			<img alt="Technorati" src="<?php echo get_stylesheet_directory_uri(); ?>/images/icon-sn-32-technorati.png" />
		</a>
	</li>
	<li class="twitter">
		<a href="<?php echo $padd_sb_twitter; ?>" class="icon-twitter" title="Twitter">
			<img alt="Twitter" src="<?php echo get_stylesheet_directory_uri(); ?>/images/icon-sn-32-twitter.png" />
		</a>
	</li>
	<li class="rss">
		<a href="<?php echo $padd_sb_feedburner; ?>" title="RSS Feed">
			<img alt="RSS" src="<?php echo get_stylesheet_directory_uri(); ?>/images/icon-sn-32-rss.png" />
		</a>
	</li>
</ul>
<?php
}

/**
 * Renders the banner advertisement
 */
function padd_widget_funct_banner() {
	global $ad_default_728;
	$sqbtn_1 = unserialize(get_option(PADD_THEME_SLUG . '_ads_728090_1'));
	$sqbtn_1 = $sqbtn_1->is_empty() ? $ad_default_728 : $sqbtn_1; 
	echo $sqbtn_1;
}

/**
 * Renders the advertisements.
 */
function padd_widget_funct_sponsors() {
	global $ad_default_125;
	$sqbtn_1 = unserialize(get_option(PADD_THEME_SLUG . '_ads_125125_1'));
	$sqbtn_1 =$sqbtn_1->is_empty()  ? $ad_default_125 : $sqbtn_1; 
	$sqbtn_1->set_css_class('ads1');
	$sqbtn_2 = unserialize(get_option(PADD_THEME_SLUG . '_ads_125125_2'));
	$sqbtn_2 = $sqbtn_2->is_empty() ? $ad_default_125 : $sqbtn_2; 
	$sqbtn_2->set_css_class('ads2');
	$sqbtn_3 = unserialize(get_option(PADD_THEME_SLUG . '_ads_125125_3'));
	$sqbtn_3 = $sqbtn_3->is_empty() ? $ad_default_125 : $sqbtn_3; 
	$sqbtn_3->set_css_class('ads3');
	$sqbtn_4 = unserialize(get_option(PADD_THEME_SLUG . '_ads_125125_4'));
	$sqbtn_4 = $sqbtn_4->is_empty() ? $ad_default_125 : $sqbtn_4; 
	$sqbtn_4->set_css_class('ads4');
	echo '<span>' . $sqbtn_1 . '</span><span>' . $sqbtn_2 . '</span><span>'. $sqbtn_3 . '</span><span>' . $sqbtn_4 . '</span>';
	echo '<div class="clear"></div>';
}

/**
 * Renders the featured posts in home page.
 */
function padd_widget_funct_featured_video() {
	wp_reset_query();	
	$featured = get_option(PADD_THEME_SLUG . '_video_cat_id','1');
	$count = '1';
	query_posts('showposts=' . $count . '&cat=' . $featured  .'&orderby=ID&order=DESC');
?>
<?php while (have_posts()) : the_post(); ?>
		<?php 
			$customfields = get_post_custom(); 
			$code = $customfields['_' . PADD_THEME_SLUG . '_post_thumbnail_video_ytube'][0]
		?>
		<object width="<?php echo PADD_YTUBE_W; ?>" height="<?php echo PADD_YTUBE_H; ?>">
			<param name="movie" value="http://www.youtube.com/v/<?php echo $code; ?>&amp;hl=en_US&amp;fs=1?rel=0"></param>
			<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
			<embed src="http://www.youtube.com/v/<?php echo $code; ?>&amp;hl=en_US&amp;fs=1?rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="<?php echo PADD_YTUBE_W; ?>" height="<?php echo PADD_YTUBE_H; ?>"></embed>
		</object>
<?php endwhile; ?>
<?php
	wp_reset_query();
}

/**
 * Sets the default menu arguments.
 */
function padd_page_menu_args($args) {
	$args['show_home'] = true;
	$args['container'] = false;
	return $args;
}
add_filter('wp_page_menu_args','padd_page_menu_args');

/**
 * Renders the list of bookmarks.
 */
function padd_theme_list_bookmarks() {
	$array = array();
	$array[] = 'category_before=';
	$array[] = 'category_after=';
	$array[] = 'categorize=0';
	$array[] = 'title_li=';
	wp_list_bookmarks(implode('&',$array));
}


/**
 * Renders the "More" string in excerpt.
 */
function padd_theme_excerpt_index_more() {
	return '...';
}
add_filter('excerpt_more','padd_theme_excerpt_index_more');

/**
 * Customize excerpt for Social Bookmarking
 */
function padd_theme_excerpt_bookmark($text) {
	return strip_tags($text);
}

/**
 * Removes the "More" string in excerpt for Social Bookmarking.
 */
function padd_theme_excerpt_bookmark_more() {
	return '...';
}

/**
 * Sets the excerpt length in index page.
 */
function padd_theme_excerpt_index_length($length) {
	return 55;
}

/**
 * Sets the excerpt length for featurd posts.
 */
function padd_theme_excerpt_featured_length($length) {
	return 48;
}

/**
 * Sets the excerpt length for pages.
 */
function padd_theme_excerpt_pages_length($length) {
	return 35;
}


/**
 * Renders the list of comments.
 *
 * @param string $comment
 * @param string $args
 * @param string $depth
 */
function padd_theme_list_comments($comment, $args, $depth) {
	$GLOBALS['comment'] = $comment; ?>
	<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
		<div class="comment">
			<div class="comment-details">
				<div class="comment-author">
					<div class="comment-avatar"><?php echo get_avatar($comment,'53'); ?></div>
					<div class="comment-meta">
						<span class="author"><?php echo get_comment_author_link(); ?></span>
						<span class="time"><?php echo get_comment_date('F j, Y'); ?></span>
					</div>
					<div class="comment-actions">
						<?php edit_comment_link(__('Edit'),'<span class="edit">','</span> | ') ?>
						<?php comment_reply_link(array_merge( $args, array('respond_id' => 'reply' ,'add_below' => 'reply', 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
					</div>
					<div class="clear"></div>
				</div>
				<div class="comment-details-interior">
					<div class="comment-details-interior-wrapper">
						<?php comment_text(); ?>
						<?php if ($comment->comment_approved == '0') : ?>
						<p class="comment-notice"><?php _e('My comment is awaiting moderation.') ?></p>
						<?php endif; ?>
					</div>
				</div>
			</div>
		</div>
	<?php
}

/**
 * Render the list of trackbacks.
 *
 * @param string $comment
 * @param string $args
 * @param string $depth
 */
function padd_theme_list_trackbacks($comment, $args, $depth) {
	$GLOBALS['comment'] = $comment; ?>
	<li <?php comment_class(); ?> id="pings-<?php comment_ID() ?>">
		<?php comment_author_link(); ?>
	<?php
}

/**
 * Count comments.
 */
function padd_theme_count_comments($count) {
	if (!is_admin()) {
		global $id;
		$comments_by_type = &separate_comments(get_comments('status=approve&post_id=' . $id));
		return count($comments_by_type['comment']);
	} else {
		return $count;
	}
}
add_filter('get_comments_number', 'padd_theme_count_comments',0);


/**
 * Renders the list of children categories in a given parent category.
 *
 * @param int $cat_id
 */
function padd_theme_get_categories($cat_id) {
	if ('' != get_the_category_by_ID($cat_id)) {
		echo '<li>';
		echo '<a href="' . get_category_link($cat_id) . '">' . get_the_category_by_ID($cat_id) . '</a>';
		if ('' != (get_category_children($cat_id))) {
			echo '<ul>';
			wp_list_categories('hide_empty=0&title_li=&child_of=' . $cat_id);
			echo '</ul>';
		}
		echo '</li>';
	}
}

/**
 * Renders the list of recent comments.
 *
 * @global object $wpdb
 * @global array $comments
 * @global array $comment
 * @param int $limit
 */
function padd_theme_recent_comments($limit=5) {
	global $wpdb, $comments, $comment;

	if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
		$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $limit");
		wp_cache_add( 'recent_comments', $comments, 'widget' );
	}
	echo '<ul class="comments-recent">';
	if ( $comments ) :
		foreach ( (array) $comments as $comment) :
			echo  '<li class="comments-recent">' . sprintf(__('%1$s on %2$s'), get_comment_author_link(), '<a href="'. get_comment_link($comment->comment_ID) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
		endforeach;
	endif;
	echo '</ul>';
}

/**
 * Returns the thumbnailed image.
 */
function padd_theme_post_thumbnail() {
	$padd_image_def = get_template_directory_uri() . '/images/thumbnail.jpg';
	if (has_post_thumbnail()) {
		the_post_thumbnail();
	} else {
		echo '<img class="thumbnail" alt="Default thumbnail." src="' . $padd_image_def . '" />';
	}
}

function padd_theme_page_box($pid,$class) {
?>
<div class="box box-<?php echo $class?>">	
	<?php
		wp_reset_query();
		query_posts('page_id=' . $pid);
		the_post();
	?>
	<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
	<div class="interior">
		<p><?php echo wp_trim_excerpt(''); ?></p>
		<p class="read-more"><a href="<?php the_permalink(); ?>" title="Permanent Link to <?php the_title_attribute(); ?>">Read More</a></p>
	</div>
	<?php wp_reset_query(); ?>
</div>
<?php
}

/**
 * Renders the featured posts in home page.
 */
function padd_theme_featured_posts($exclude=array()) {
	wp_reset_query();	
	$featured = get_option(PADD_THEME_SLUG . '_featured_cat_id','1');
	$count = get_option(PADD_THEME_SLUG . '_featured_cat_limit');
	query_posts('showposts=' . $count . '&cat=' . $featured);
	$padd_image_def = get_template_directory_uri() . '/images/thumbnail-gallery.jpg';
	add_filter('excerpt_length', 'padd_theme_excerpt_featured_length');
?>
<div id="jquery-cycle">
	<div class="list">
	<?php while (have_posts()) : the_post(); ?>
		<div class="item append-clear">
			<a class="image" href="<?php the_permalink(); ?>" title="Permanent Link to <?php the_title_attribute(); ?>">
			<?php 
				$exclude[] = get_the_ID();
				if (has_post_thumbnail()) {
					the_post_thumbnail('gallery');
				} else {
					echo '<img class="thumbnail" src="' . $padd_image_def . '" />';
				}
			?>
			</a>
			<div class="meta">
				<h3><a href="<?php the_permalink(); ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
				<?php the_excerpt(); ?>
				<p class="read-more"><a href="<?php the_permalink(); ?>" title="Permanent Link to <?php the_title_attribute(); ?>">Read More</a></p>
			</div>
		</div>
	<?php endwhile; ?>
	</div>
</div>
<?php
	wp_reset_query();
	remove_filter('excerpt_length', 'padd_theme_excerpt_featured_length');
	return $exclude;
}

/**
 * Renders the TweetMeme button.
 */
function padd_tweetmeme_generate_button($style='') {
	global $post;
	$url = '';
	if (get_post_status($post->ID) == 'publish') {   
		$url = get_permalink();
	} 
	?>
	<div class="tweetmeme">
		<script type="text/javascript">
			tweetmeme_url = '<?php echo $url; ?>';
			<?php echo !empty($style) ? 'tweetmeme_style="' . $style . '"': ''; ?>
		</script>
		<script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script>
	</div>
	<?php
}

/**
 * Renders the Facebook Share button 
 */
function padd_facebookshare_generate_button() {
	global $post;
	$url = '';
	if (get_post_status($post->ID) == 'publish') {   
		$url = get_permalink();
		$title = get_the_title();
	} 
	?>
	<div class="facebookshare">
		<a name="fb_share" type="button_count" href="http://www.facebook.com/sharer.php?u=<?php echo urlencode($url); ?>&amp;t=<?php echo urlencode($title); ?>">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>
	</div>
	<?php
}

/**
 * Renders the Google Buzz button 
 */
function padd_googlebuzz_generate_button() {
	global $post;
	$url = '';
	if (get_post_status($post->ID) == 'publish') {   
		$url = get_permalink();
		$title = get_the_title();
	} 
	?>
	<div class="googlebuzz">
		<a href="http://www.google.com/reader/link?url=<?php echo urlencode($url); ?>&amp;title=<?php echo urlencode($title); ?>&amp;snippet=<?php echo urlencode(padd_theme_summary(get_the_content(),160)) ;?>&amp;srcURL=<?php echo urlencode(get_option('home')); ?>&amp;srcTitle=<?php urlencode(get_option('name')); ?>">Buzz it!</a>
	</div>
	<?php
}

/** 
 * Renders the related posts
 *
 * @param int|string $post_ID
 */
function padd_related_posts($post_ID) {
	$enabled = get_option(PADD_THEME_SLUG . '_rp_enable');
	if ($enabled) {
		
		$tag_ids = array();
		$cat_ids = array();
		
		$tags = wp_get_post_tags($post_ID);
		foreach($tags as $tag) {
			$tag_ids[] = $tag->term_id;
		}
		
		$cats = get_the_category($post_ID);
		if ($cats) {
			foreach($cats as $cat) {
				$cat_ids[] = $cat->term_id;
			}
		}

		$args = array(
					'post__not_in' => array($post_ID),
					'showposts' => intval(get_option(PADD_THEME_SLUG . '_rp_max',5)),
					'caller_get_posts' => 1
				); 
		if (!empty($tag_ids) && get_option(PADD_THEME_SLUG . '_consider_tags','1') === '1') {
			$args['tag__in'] = $tag_ids;
		}
		if (!empty($cat_ids) && get_option(PADD_THEME_SLUG . '_consider_categories','1') === '1') {
			$args['category__in'] = $cat_ids;
		}
		
		$rp_query = new wp_query($args);

		if ($rp_query->have_posts()) {
			echo '<ul>';
			while ($rp_query->have_posts()) {
				$rp_query->the_post();
			?>
				<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
			<?php
			}
			echo '</ul>';
		} else {
			echo '<p>There are no related posts on this entry.</p>';
		}
	} else {
		echo '<p>Related posts has been disabled.</p>';
	}
	// That should fix the bug in the single.php.
	wp_reset_query();
}

/** 
 * Renders the theme credits.
 */
function padd_theme_credits() {
	do_action(__FUNCTION__);
}